function ArticleList() {

}

/**
 * 监听添加评论点击事件
 */
ArticleList.prototype.listenSubmitEvent = function () {
    var btn = $('.submit-btn');
    var textarea = $("textarea[name='comment']");
    btn.click(function () {
        var content = textarea.val();
        var article_id = btn.attr('data-article-id');
        myajax.post({
            url: '/article/public_comment/',
            data: {
                content,
                article_id
            },
            success: function (result) {
                if (result['code'] === 200) {
                    var comment = result['data'];
                    var tpl = template('comment-item', {comment});
                    var commentListGroup = $('.parent-comment-list');
                    commentListGroup.prepend(tpl);
                    window.messageBox.showSuccess('评论发表成功');
                    textarea.val('');
                }
            }
        });
    });
};

/**
 * 点击回复按钮后触发
 */
ArticleList.prototype.listenReplyClickEvent = function () {
    var all_btn_div = $('.more-opt');
    var article_id = $('#submit-btn').attr('data-article-id');
    all_btn_div.each(function (index, ele) {
        var btn = $(ele).children('button');
        btn.click(function () {
            var comment_id = $(this).attr('data-id');
            var reply_user_id = $(this).attr('data-reply-user-id');
            linalert.alertOneInput({
                'title': '请输入评论',
                'placeholder': '请输入评论内容',
                'confirmCallback': function (inputVal) {
                    if (inputVal == '' || inputVal.trim() == ''){
                        window.messageBox.showInfo('评论内容不能为空');
                        return;
                    }
                    myajax.post({
                        url: '/article/public_comment/',
                        data: {
                            article_id,
                            comment_id,
                            reply_user_id,
                            content: inputVal
                        },
                        success: function(result){
                            if (result['code'] === 200){
                                var element = $(ele);
                                var comment = result['data'];
                                var tpl = template('sub-comment-item', {comment});
                                //length为0代表没有兄弟标签ul，需要创建
                                var check_length = element.siblings('.sub-comment-list').length;
                                if (check_length === 0){
                                    //创建ul标签插入
                                    var margin_left = parseInt(element.parent().parent().css('margin-left').split('px')[0]) + 15 + 'px';
                                    var ul_ele = $('<ul style="margin-left: ' + margin_left + '"></ul>');
                                    ul_ele.prepend(tpl);
                                    element.parent().append(ul_ele);
                                }else{
                                    element.parent().parent().prepend(tpl);
                                }
                                window.messageBox.showSuccess('评论发表成功');
                                linalert.close();
                            }
                        }
                    });
                }
            });
        })
    });
};

/**
 * 入口函数
 */
ArticleList.prototype.run = function () {
    this.listenSubmitEvent();
    this.listenReplyClickEvent();
};

$(function () {
    var a = new ArticleList();
    a.run();
});
